package com.douyuehan.doubao.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 自定义redis连接池JedisPool，并注入spring容器
 */
@Service
public class RedisPoolFactory {

	@Autowired
	RedisConfig  redisConfig;

	/**
	 * 将redis连接池注入spring容器
	 * @return
	 */
	@Bean
	public JedisPool JedisPoolFactory(){
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxIdle(redisConfig.getPoolMaxIdle());
		config.setMaxTotal(redisConfig.getPoolMaxTotal());
		config.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);
		// 连接池参数设置
		JedisPool jp = new JedisPool(config, redisConfig.getHost(), redisConfig.getPort(),
				redisConfig.getTimeout()*1000, redisConfig.getPassword(), 0);
		return jp;
	}
}